﻿Public Class LABGROUP
    Private LAB_GROUP_CODE = Nothing
    Private LAB_GROUP_NAME = Nothing
    Private LAB_GROUP_SHORT_NAME = Nothing
    Private DIS_ORDER = Nothing
    Private STATUS = Nothing
    Private selectLabgroup = "SELECT `LAB_GROUP_CODE` as 'รหัสกลุ่ม lab', `LAB_GROUP_NAME` as 'ชื่อกลุ่ม lab', `LAB_GROUP_SHORT_NAME` as 'ชื่อย่อกลุ่ม lab', `DIS_ORDER` as 'ลำดับ', `STATUS` as 'สถานะการเปิดใช้งาน' FROM lab_group ORDER BY DIS_ORDER ASC"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    'Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property LAB_GROUP_CODE_ As Integer
        Get
            Return LAB_GROUP_CODE
        End Get
        Set(value As Integer)
            LAB_GROUP_CODE = value
        End Set
    End Property

    Property LAB_GROUP_NAME_ As String
        Get
            Return LAB_GROUP_NAME
        End Get
        Set(value As String)
            LAB_GROUP_NAME = value
        End Set
    End Property

    Property LAB_GROUP_SHORT_NAME_ As String
        Get
            Return LAB_GROUP_SHORT_NAME
        End Get
        Set(value As String)
            LAB_GROUP_SHORT_NAME = value
        End Set
    End Property

    Property DIS_ORDER_ As Integer
        Get
            Return DIS_ORDER
        End Get
        Set(value As Integer)
            DIS_ORDER = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectLabgroup_ As String
        Get
            Return selectLabgroup
        End Get
        Set(value As String)

        End Set
    End Property

    Public Sub moveOrder(typeMove As String, currentOrder As Integer)
        Dim maxOrder As Integer
        maxOrder = connect.GetTable("SELECT MAX(DIS_ORDER) FROM lab_group WHERE LAB_GROUP_CODE <> 100").Rows(0)(0)

        If (currentOrder = 1 And typeMove = "up") Or (currentOrder = maxOrder And typeMove = "down") Then
            If currentOrder = 1 Then
                MsgBox("ไม่สามารถเลื่อนตำแหน่งได้ เนื่องจากตำแหน่งปัจจุบันเป็นตำแหน่งแรกสุด")
            ElseIf currentOrder = maxOrder Then
                MsgBox("ไม่สามารถเลื่อนตำแหน่งได้ เนื่องจากตำแหน่งปัจจุบันเป็นตำแหน่งท้ายสุด")
            End If
        Else
            If typeMove = "up" Then
                Dim newOrder As Integer
                newOrder = currentOrder - 1

                Dim IDToMoveDown As Integer
                IDToMoveDown = connect.GetTable("SELECT LAB_GROUP_CODE FROM lab_group WHERE DIS_ORDER = '" & newOrder & "'").Rows(0)(0)

                Dim IDToMoveUp As Integer
                IDToMoveUp = connect.GetTable("SELECT LAB_GROUP_CODE FROM lab_group WHERE DIS_ORDER = '" & currentOrder & "'").Rows(0)(0)

                connect.ExecuteNonQuery("UPDATE lab_group SET DIS_ORDER = '" & currentOrder & "' WHERE LAB_GROUP_CODE = '" & IDToMoveDown & "'")
                connect.ExecuteNonQuery("UPDATE lab_group SET DIS_ORDER = '" & newOrder & "' WHERE LAB_GROUP_CODE = '" & IDToMoveUp & "'")
            End If

            If typeMove = "down" Then
                Dim newOrder As Integer
                newOrder = currentOrder + 1

                Dim IDToMoveDown As Integer
                IDToMoveDown = connect.GetTable("SELECT LAB_GROUP_CODE FROM lab_group WHERE DIS_ORDER = '" & currentOrder & "'").Rows(0)(0)

                Dim IDToMoveUp As Integer
                IDToMoveUp = connect.GetTable("SELECT LAB_GROUP_CODE FROM lab_group WHERE DIS_ORDER = '" & newOrder & "'").Rows(0)(0)

                connect.ExecuteNonQuery("UPDATE lab_group SET DIS_ORDER = '" & newOrder & "' WHERE LAB_GROUP_CODE = '" & IDToMoveDown & "'")
                connect.ExecuteNonQuery("UPDATE lab_group SET DIS_ORDER = '" & currentOrder & "' WHERE LAB_GROUP_CODE = '" & IDToMoveUp & "'")
            End If
        End If
    End Sub

    Public Sub insertLab_groupData()
        If TypeOf LAB_GROUP_NAME Is String And TypeOf LAB_GROUP_SHORT_NAME Is String Then
            If connect.GetTable("SELECT LAB_GROUP_CODE FROM lab_group WHERE LAB_GROUP_NAME = '" & LAB_GROUP_NAME & "' OR LAB_GROUP_SHORT_NAME = '" & LAB_GROUP_SHORT_NAME & "'").Rows.Count > 0 Then
                MsgBox("มีข้อมูลกลุ่ม lab นี้อยู่ในระบบแล้ว")
            Else
                Try
                    connect = CONDB.NewConnection()
                    'connectlocal = conlocal.NewConnection()
                    'Dim dischargeID As Integer

                    Dim labGroupCode As Integer
                    labGroupCode = connect.GetTable("SELECT MAX(LAB_GROUP_CODE) + 1 FROM lab_group WHERE LAB_GROUP_CODE <> 100").Rows(0)(0)

                    Dim labDisorder As Integer
                    labDisorder = connect.GetTable("SELECT MAX(DIS_ORDER) + 1 FROM lab_group WHERE LAB_GROUP_CODE <> 100").Rows(0)(0)

                    connect.ExecuteNonQuery("INSERT INTO lab_group (LAB_GROUP_CODE, LAB_GROUP_NAME, LAB_GROUP_SHORT_NAME, DIS_ORDER, STATUS) VALUES ('" & labGroupCode & "', '" & LAB_GROUP_NAME & "', '" & LAB_GROUP_SHORT_NAME & "', '" & labDisorder & "', '" & STATUS & "')")
                    'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                    'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                    MsgBox("ท่านได้ทำการเพิ่มข้อมูลกลุ่ม lab เรียบร้อยแล้ว")
                Catch ex As Exception
                    MsgBox(vbCrLf & ex.Message)
                End Try
                connect.Dispose()
                'connectlocal.Dispose()
            End If
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง ต้องเป็นตัวอักษร")
        End If
    End Sub

    Public Sub editLab_groupData()
        If TypeOf LAB_GROUP_NAME Is String And TypeOf LAB_GROUP_SHORT_NAME Is String Then
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("UPDATE lab_group SET LAB_GROUP_CODE = '" & LAB_GROUP_CODE & "', LAB_GROUP_NAME = '" & LAB_GROUP_NAME & "', LAB_GROUP_SHORT_NAME = '" & LAB_GROUP_SHORT_NAME & "', STATUS = '" & STATUS & "' WHERE LAB_GROUP_CODE = '" & LAB_GROUP_CODE & "'")
                'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
                MsgBox("ท่านได้ทำการแก้ไขข้อมูลกลุ่ม lab เรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง ต้องเป็นตัวอักษร")
        End If
    End Sub
End Class
